<template>
  <div>
	<a-row type="flex rows2">
		<a-col :flex="0" class="rows">_api_key</a-col>
		<a-col :flex="4"><a-input v-model="apiKey" @input="input" placeholder="请输入API Key" /></a-col>
		<a-col :flex="2"></a-col>
	</a-row> 
	<div style="display: flex;
    flex-wrap: wrap;">
		<div v-for="item in packageList" style="margin: 15px;">
				  <h3>{{item.name}}</h3>
				  <img :src="item.qrCode">
		</div>
	</div>
	  <!-- <h3>安卓下载二维码</h3>
	  <img src="../../assets/logo.png"> -->

    <!-- <h3 :style="{ marginBottom: '16px' }">
      demo1 打开文件夹实现
    </h3>
    <a-list bordered :data-source="data">
      <a-list-item @click="openDirectry(item.id)" slot="renderItem" slot-scope="item">
        {{ item.content }}
        <a-button type="link">
          打开
        </a-button>
      </a-list-item>
    </a-list> -->
  </div>
</template>
<script>
import { openDir,showScanCode,listMy } from '@/api/main'

const data = [
  {
    content: '【下载】目录',
    id: 'download'
  },
  {
    content: '【图片】目录',
    id: 'picture'
  },
  {
    content: '【文档】目录',
    id: 'doc'
  },
  {
    content: '【音乐】目录',
    id: 'music'
  }
];

export default {
  data() { 
    return {
      data,
	  packageList:[],
	  apiKey:''
    };
  },
  methods: { 
	  input(e) {
		  console.log(e)
		  localStorage.setItem('apiKey',this.apiKey)
	  },
    openDirectry (id) {
      console.log('id:', id)
      const params = {
        'id': id
      }
      openDir(params).then(res => {
        if (res.code !== 0) {
          return false
        }

        }).catch(err => {
          console.log('err:', err)
        })
    },
	loadImg() {
		let data = {
			_api_key:this.apiKey,
		}
		let ret = '';
		for (let it in data) {
			ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
		}
		ret = ret.substr(0,ret.length-1)
		listMy(ret).then(res => {
		  if (res.code !== 0) {
		    return false
		  }
		  res.data.list.map(item => {
			  let data = {
			  	_api_key:this.apiKey,
				appKey:item.appKey
			  }
			  let ret = '';
			  for (let it in data) {
			  	ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
			  }
			  ret = ret.substr(0,ret.length-1)
			  showScanCode(ret).then(success => {
			    if (res.code !== 0) {
			      return false
			    }			  	
			    this.packageList.push({
					name:`${success.data.buildName} ${success.data.buildType==1?'ios版':'android版'}`,
					qrCode:success.data.buildQRCodeURL
				})
			    }).catch(err => {
			  	console.log('err:', err)
			    })
		  })

		  }).catch(err => {
			console.log('err:', err)
		  })
	}
  },
  mounted () {
	this.apiKey = localStorage.getItem('apiKey')
    this.loadImg()
  }
};
</script>
<style></style>
